package com.zjxny.jobs;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;
import xyz.erupt.core.annotation.EruptHandlerNaming;
import xyz.erupt.job.handler.EruptJobHandler;

@Slf4j
@Service
@EruptHandlerNaming("修复短信发送历史中异常数据")
public class FixSmsJob implements EruptJobHandler {
	
	@Resource
	JdbcTemplate jdbcTemplate;
	
	@Override
	public String exec(String code, String param) {
//		String sql = "SELECT * FROM sms_history WHERE `status`=-1 and error_msg='isv.SMS_TEMPLATE_ILLEGAL|请检查模板内容与模板参数是否匹配'";
		String sql = "SELECT * FROM sms_history WHERE `status`=-1 and hphm<>'粤BAL3881' and id> 1286 order by id desc";
		//粤BAL3881
		List<Map<String,Object>> lists = jdbcTemplate.queryForList(sql);
		
		int i = 0;
		for(Map<String,Object> map: lists) {
			String hphm =(String) map.get("hphm");
			Long id = (Long) map.get("id");
			String content = (String) map.get("content");
			
			String sqlExt = "select * from sms_info where hphm='"+hphm+"'";
			List<Map<String,Object>> traffics = jdbcTemplate.queryForList(sqlExt);
//			System.err.println(JSONUtil.toJsonPrettyStr(traffics));
			Long trafficId = 0l;
			String xh="";
			for(Map<String,Object> traffic: traffics) {
				String wfdz = (String) traffic.get("wfdz");
				String contentExt = (String) traffic.get("content");
//				System.err.println(contentExt+"\t"+content);
//				if(content.contains("在"+wfdz+"违法")) {

				if(contentExt.equals(content)) {
					i++;
					trafficId = (Long)traffic.get("id");
					System.err.println("trafficId="+trafficId);
					if("粤BD00519".equals(hphm)) {
						System.err.println("---here---");
					}
					
//					xh=(String) traffic.get("xh");
//					int affectNum = jdbcTemplate.update("update sms_history set update_time=now(), xh='"+xh+"' where id="+id);
//					int affectNumExt = jdbcTemplate.update("update sms_info set update_time=now(), status=0 where id="+trafficId);
//					log.info("hphm={},xh={},affectNum={},affectNumExt={}",hphm,xh,affectNum,affectNumExt);
					break;
				}
			}
		}
		
		System.err.println("affect.total="+i);
		return null;
	}
}